package com.tx.admin.detailmodehead.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tx.core.util.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.tx.admin.detailmodehead.service.DetailModeHeadService;
import com.tx.admin.detailmodehead.pojo.po.DetailModeHead;
import com.tx.admin.detailmodehead.mapper.DetailModeHeadMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryChain;
import com.tx.core.common.enums.CommonResponseEnum;
import com.tx.core.common.entity.PageResult;
import com.tx.core.common.entity.SelectIdsDTO;
import java.io.Serializable;
import java.util.List;
import com.tx.admin.detailmodehead.pojo.dto.DetailModeHeadCreateDTO;
import com.tx.admin.detailmodehead.pojo.dto.DetailModeHeadUpdateDTO;
import com.tx.admin.detailmodehead.pojo.dto.DetailModeHeadListDTO;
import com.tx.admin.detailmodehead.pojo.dto.DetailModeHeadImportDTO;
import com.tx.core.common.entity.ImportExcelDTO;
import com.tx.excel.core.ExcelResult;
import java.io.OutputStream;
import jakarta.servlet.http.HttpServletResponse;
import com.tx.excel.utils.ExcelUtils;
import lombok.SneakyThrows;
import com.tx.admin.detailmodehead.pojo.vo.DetailModeHeadVO;

/**
 * <p>
 * 详情页模板头部 服务实现类
 * </p>
 *
 * @author sz-admin
 * @since 2025-08-05
 */
@Service
@RequiredArgsConstructor
public class DetailModeHeadServiceImpl extends ServiceImpl<DetailModeHeadMapper, DetailModeHead> implements DetailModeHeadService {
    @Override
    public void create(DetailModeHeadCreateDTO dto){
        DetailModeHead detailModeHead = BeanCopyUtils.copy(dto, DetailModeHead.class);
        detailModeHead.setId(System.currentTimeMillis());
        save(detailModeHead);
    }

    @Override
    public void update(DetailModeHeadUpdateDTO dto){
        DetailModeHead detailModeHead = BeanCopyUtils.copy(dto, DetailModeHead.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create()
            .eq(DetailModeHead::getId, dto.getId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);

        saveOrUpdate(detailModeHead);
    }

    @Override
    public PageResult<DetailModeHeadVO> page(DetailModeHeadListDTO dto){
        Page<DetailModeHeadVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto), DetailModeHeadVO.class);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<DetailModeHeadVO> list(DetailModeHeadListDTO dto){
        return listAs(buildQueryWrapper(dto), DetailModeHeadVO.class);
    }

    @Override
    public void remove(SelectIdsDTO dto){
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public DetailModeHeadVO detail(Object id){
        DetailModeHead detailModeHead = getById((Serializable) id);
        CommonResponseEnum.INVALID_ID.assertNull(detailModeHead);
        return BeanCopyUtils.copy(detailModeHead, DetailModeHeadVO.class);
    }

    @SneakyThrows
    @Override
    public void importExcel(ImportExcelDTO dto) {
        ExcelResult<DetailModeHeadImportDTO> excelResult = ExcelUtils.importExcel(dto.getFile().getInputStream(), DetailModeHeadImportDTO.class, true);
        List<DetailModeHeadImportDTO> list = excelResult.getList();
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
        System.out.println(" isCover : " + dto.getIsCover());
    }

    @SneakyThrows
    @Override
    public void exportExcel(DetailModeHeadListDTO dto, HttpServletResponse response) {
        List<DetailModeHeadVO> list = list(dto);
        String fileName = "详情页模板头部模板";
        OutputStream os = FileUtils.getOutputStream(response, fileName + ".xlsx");
        ExcelUtils.exportExcel(list, "详情页模板头部", DetailModeHeadVO.class, os);
    }

    private static QueryWrapper buildQueryWrapper(DetailModeHeadListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(DetailModeHead.class);
        if (Utils.isNotNull(dto.getTitle())) {
            wrapper.eq(DetailModeHead::getTitle, dto.getTitle());
        }
        if (Utils.isNotNull(dto.getTitleUnder())) {
            wrapper.eq(DetailModeHead::getTitleUnder, dto.getTitleUnder());
        }
        if (Utils.isNotNull(dto.getStatus())) {
            wrapper.eq(DetailModeHead::getStatus, dto.getStatus());
        }
        if (Utils.isNotNull(dto.getContext())) {
            wrapper.eq(DetailModeHead::getContext, dto.getContext());
        }
        if (Utils.isNotNull(dto.getMusicFile())) {
            wrapper.eq(DetailModeHead::getMusicFile, dto.getMusicFile());
        }
        if (Utils.isNotNull(dto.getExt1())) {
            wrapper.eq(DetailModeHead::getExt1, dto.getExt1());
        }
        if (Utils.isNotNull(dto.getExt2())) {
            wrapper.eq(DetailModeHead::getExt2, dto.getExt2());
        }
        if (Utils.isNotNull(dto.getExt3())) {
            wrapper.eq(DetailModeHead::getExt3, dto.getExt3());
        }
        return wrapper;
    }
}